import express, { Request, Response, NextFunction } from 'express';
import createError from 'http-errors';
import { User } from '../model';
import { INTERNAL_SERVER_ERROR } from 'http-status-codes';
var router = express.Router();

router.get('/', async function (_req: Request, res: Response, next: NextFunction) {
    try {
        let users = await User.findAll();
        res.json({
            success: true,
            data: users
        })
    } catch (error) {
        next(createError(INTERNAL_SERVER_ERROR));
    }
});
router.get('/:id', async function (req: Request, res: Response, next: NextFunction) {
    try {
        let user = await User.findByPk(req.params.id);
        if (!user) {
            return next(createError(INTERNAL_SERVER_ERROR));
        }
        res.json({
            success: true,
            data: user
        })
    } catch (error) {
        next(createError(INTERNAL_SERVER_ERROR));
    }

});
router.post('/', async function (req: Request, res: Response, next: NextFunction) {
    try {
        let user = req.body;
        user = await User.create(user);
        res.json({
            success: true,
            data: user
        })
    } catch (error) {
        next(createError(INTERNAL_SERVER_ERROR));
    }

});
router.put('/:id', async function (req: Request, res: Response, next: NextFunction) {
    try {
        let id = req.params.id;
        let update = req.body;
        let user = await User.findByPk(id);
        if (!user) {
            return next(createError(INTERNAL_SERVER_ERROR));
        }
        user = await user.update(update);
        res.json({
            success: true,
            data: user
        })
    } catch (error) {
        next(createError(INTERNAL_SERVER_ERROR));
    }

});
router.delete('/:id', async function (req: Request, res: Response, next: NextFunction) {
    try {
        let id = req.params.id;
        let user = await User.findByPk(id);
        if (!user) {
            return next(createError(INTERNAL_SERVER_ERROR));
        }
        user = await user.destroy();
        res.json({
            success: true,
            data: user
        })
    } catch (error) {
        next(createError(INTERNAL_SERVER_ERROR));
    }
});

module.exports = router;
